package org.itboys.mobile.controller.mobile;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayCountDto;
import org.itboys.mobile.service.mongo.mobile.SundayMobileCountService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：jiangxiong
 * 日期：2017年2月9日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_结算（确认订单）_controller。
 *
 */
@Controller
@RequestMapping("/tianjie/mobile/finance/globe/count")
public class SundayMobileCountController extends BaseController {
    @Autowired
    private SundayMobileCountService countService;

    /**
     * 【用户版和管理版】购物车结算（确认订单）。如果使用了优惠券，余额，或切换了配送方式。请重新调用接口加载数据
     * @param request
     * @param response
     * @param cartIds

     */
    @RequestMapping(value="/countCart",method= {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value="【全球购-结算】购物车结算(确认订单)")
    public void countCart(HttpServletRequest request,
             HttpServletResponse response,
                          @ApiParam(required = true, name = "cartIds", value = "购物车ID集合(非商品ID)。如：cardIds=1,2,3。）") @RequestParam(value = "cartIds", required = true) List<Long> cartIds,
          @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
          @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign) {

        try {
            Long memberId = AccountIdHoldUtils.getMemberId();
            SundayCountDto result = countService.countCart(memberId, cartIds, SundayCommonConstants.cart_type_cart);
            ResponseMessage.success(result);
            //JsonPageUtils.renderJson(response,houses);
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/tianjie/mobile/finance/globe/count/countCart----e=" + e.getMessage());
                e.printStackTrace();
                ResponseMessage.error(code, msg);
            }
        }
    }
    /**
     * 【用户版和管理版】立即购买结算（确认订单）。如果使用了优惠券，余额，或切换了配送方式。请重新调用接口加载数据
     * @param request
     * @param response
     * @param productId
     * @param number
     */
    @RequestMapping(value="/countBuyNow",method= {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value="【全球购-结算】立即购买结算(确认订单)")
    public void countBuyNow(HttpServletRequest request,
                        HttpServletResponse response,
                    @ApiParam(required = true, name = "productId", value = "商品ID") @RequestParam(value = "productId", required = true)Long  productId,
                    @ApiParam(required = true, name = "number", value = "购买数量）") @RequestParam(value = "number", required = true) Integer number,
                    @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                    @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign) {
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            SundayCountDto result = countService.countBuyNow(memberId, productId, number);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/mobile/finance/globe/count/countBuyNow----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }


}
